﻿//-----------------------------------------------------------------------
// <copyright file="FontSizeSlider.xaml.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 
namespace ChatBox.UI.Controls
{
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Controls;

    /// <summary>
    /// Interaction logic for FontSizeSlider.xaml
    /// </summary>
    public partial class FontSizeSlider : UserControl
    {
        /// <summary>
        /// The "SliderFontSize" Dependency Property
        /// </summary>
        public static readonly DependencyProperty SliderFontSizeProperty = DependencyProperty.Register(
            "SliderFontSize", typeof(double), typeof(FontSizeSlider), new PropertyMetadata(new PropertyChangedCallback(SliderFontSizeChanged)));

        /// <summary>
        /// The base text
        /// </summary>
        private string title;

        /// <summary>
        /// Initializes a new instance of the FontSizeSlider class
        /// </summary>
        public FontSizeSlider()
        {
            InitializeComponent();

            // set up defaults for general readable range of fonts
            this.Minimum = 8;
            this.Maximum = 25;

            this.sizeSlider.ValueChanged += this.SizeSliderValueChanged;
        }

        /// <summary>
        /// Gets or sets the font minimum size
        /// </summary>
        public double Minimum
        {
            get
            {
                return this.sizeSlider.Minimum;
            }

            set
            {
                this.sizeSlider.Minimum = value;
            }
        }

        /// <summary>
        /// Gets or sets the font maximum size
        /// </summary>
        public double Maximum
        {
            get
            {
                return this.sizeSlider.Maximum;
            }

            set
            {
                this.sizeSlider.Maximum = value;
            }
        }

        /// <summary>
        /// Gets or sets the text displayed
        /// </summary>
        public string Title
        {
            get
            {
                return this.title;
            }

            set
            {
                this.title = value;
                this.titleTextBlock.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets the current font size value. Dependency property
        /// </summary>
        public double SliderFontSize
        {
            get
            {
                double value = (double)this.GetValue(SliderFontSizeProperty);
                return Math.Round(value, 1);
            }

            set
            {
                value = Math.Round(value, 1);
                this.SetValue(SliderFontSizeProperty, value);
            }
        }

        /// <summary>
        /// Dependency property changed callback
        /// </summary>
        /// <param name="source">the target object</param>
        /// <param name="e">the args containing the new value</param>
        private static void SliderFontSizeChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            FontSizeSlider fontSizeSlider = source as FontSizeSlider;
            if (fontSizeSlider != null)
            {
                fontSizeSlider.SliderFontSizeChanged((double)e.NewValue);
            }
        }

        /// <summary>
        /// Dependency property changed callback on an instance
        /// </summary>
        /// <param name="value">the new value for the slider</param>
        private void SliderFontSizeChanged(double value)
        {
            this.sizeSlider.Value = value;
            this.SetSampleText();
        }

        /// <summary>
        /// set the actual text
        /// </summary>
        private void SetSampleText()
        {
            this.displaySampleText.Text = "Sample " + this.SliderFontSize.ToString("0.0", CultureInfo.InvariantCulture) + "pt";
        }

        /// <summary>
        /// Event handler for the slider control changed
        /// </summary>
        /// <param name="sender">the event sender</param>
        /// <param name="e">the even parameter</param>
        private void SizeSliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.SliderFontSize = e.NewValue;
        }
    }
}
